package com.gcb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.gcb.common.enums.CreateTypeEnum;
import com.gcb.common.enums.OrderStateEnum;
import com.gcb.common.enums.PriceTypeEnum;
import com.gcb.common.utils.OrderUtils;
import com.gcb.common.utils.PriceUtil;
import com.gcb.domain.dto.OrderCargoQueryDTO;
import com.gcb.domain.dto.OrderCargoSaveDTO;
import com.gcb.domain.entity.Company;
import com.gcb.domain.entity.OrderCargo;
import com.gcb.domain.vo.OrderCargoVO;
import com.gcb.mapper.CompanyMapper;
import com.gcb.mapper.OrderCargoMapper;
import com.gcb.service.IOrderCargoService;
import com.gcb.service.IPortService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class OrderCargoServiceImpl implements IOrderCargoService {

    @Resource
    private OrderCargoMapper orderCargoMapper;
    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private IPortService portService;

    @Override
    public void add(OrderCargoSaveDTO dto, Long currentUserId) {
        OrderCargo orderCargo = new OrderCargo();
        BeanUtil.copyProperties(dto, orderCargo);
        Company company = companyMapper.selectCompanyById(dto.getCompanyId());
        Long operateId = company.getOperateId();
        if (Objects.nonNull(operateId)) {
            orderCargo.setOperatorId(operateId);
        }

        // 生成订单号
        String orderNo = OrderUtils.generateOrderNo("G");
        orderCargo.setOrderNo(orderNo);
        // 设置订单状态 当前没有审核流程，直接审核通过，状态为进行中
        orderCargo.setState(OrderStateEnum.UNDERWAY.getCode());

        orderCargo.setCreateBy(currentUserId);
        orderCargo.setUpdateBy(currentUserId);
        //如果价格为空，那么默认为0
        if (Objects.isNull(dto.getPrice())){
            orderCargo.setPrice("0");
        }else {
            orderCargo.setPrice(PriceUtil.changeY2F(dto.getPrice()+"")+"");
        }
        orderCargoMapper.insert(orderCargo);
    }

    @Override
    public void update(OrderCargoSaveDTO dto, Long currentUserId) {
        OrderCargo orderCargo = new OrderCargo();
        BeanUtil.copyProperties(dto, orderCargo);

        Company company = companyMapper.selectCompanyById(dto.getCompanyId());
        orderCargo.setOperatorId(company.getOperateId());

        orderCargo.setUpdateBy(currentUserId);
        orderCargoMapper.update(orderCargo);
    }

    @Override
    public List<OrderCargoVO> list(OrderCargoQueryDTO dto) {
        // 根据城市搜索，先根据城市获取港口列表，这样可以少关联数据库表
        if (StrUtil.isNotBlank(dto.getCityName())) {
            List<String> portList = portService.selectNameByCity(dto.getCityName());
            if (portList != null && !portList.isEmpty()) {
                dto.setPortList(portList);
            }
        }
        List<OrderCargoVO> list = orderCargoMapper.list(dto);
        list.forEach(this::handleVO);
        return list;
    }

    private void handleVO(OrderCargoVO item) {
        // 枚举转换
        item.setStateStr(OrderStateEnum.getInfo(item.getState()));
        item.setCreateTypeStr(CreateTypeEnum.getInfo(item.getCreateType()));
        item.setPriceTypeStr(PriceTypeEnum.getInfo(item.getPriceType()));
        item.setPrice(PriceUtil.changeF2Y(item.getPrice()));
    }

    @Override
    public OrderCargoVO getDetail(Long id) {
        OrderCargoVO orderCargoVO = orderCargoMapper.getById(id);
        handleVO(orderCargoVO);
        return orderCargoVO;
    }

    @Override
    public void batchUpdateState(List<Long> ids, Integer state, Long currentUserId) {
        orderCargoMapper.batchUpdateState(ids, state, currentUserId);
    }
}
